package com.web.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

/**
 * 自定义网关过滤器
 */
@Component
public class HelloGatewayFilterFactory
        extends AbstractGatewayFilterFactory<HelloGatewayFilterFactory.Config> {

    public HelloGatewayFilterFactory(){
        //传入参数类的类型
        super(Config.class);
    }

    /**
     * 执行过滤逻辑
     */
    @Override
    public GatewayFilter apply(Config config) {
        //使用lambda表达式实现GatewayFilter接口，并返回
        return (exchange, chain) -> {
            System.out.println("Hello! ----------------> " + config.getNickname());
            //放行请求，到下一个过滤器
            return chain.filter(exchange);
        };
    }

    /**
     * 自定义参数类
     */
    public static class Config{
        //参数
        private String nickname;
        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
    }
}
